﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
using System.Windows.Forms;



namespace AllInOne.UsbCom
{
    /// <summary>
    /// 扫描枪（串口 ）工作类
    /// </summary>
    public class CommBar : IDisposable
    {
        /// <summary>
        /// 串口引用
        /// </summary>
        public SerialPort serialPort;

        /// <summary>
        /// 存储转换的数据值
        /// </summary>
        public string Code { get; set; }

        /// <summary>
        /// 扫描枪（串口 ）工作类
        /// </summary>
        public CommBar()
        {
            serialPort = new SerialPort();
        }

        /// <summary>
        /// 串口状态
        /// </summary>
        public bool IsOpen
        {
            get
            {
                return serialPort.IsOpen;
            }
        }

        /// <summary>
        /// 打开串口
        /// </summary>
        /// <returns></returns>
        public bool Open()
        {
            if (serialPort.IsOpen)
            {
                Close();
            }
            serialPort.Open();
            if (serialPort.IsOpen)
            {
                return true;
            }
            else
            {
                MessageBox.Show("串口打开失败！");
                return false;
            }
        }

        /// <summary>
        /// 关闭串口
        /// </summary>
        public void Close()
        {
            serialPort.Close();
        }

        /// <summary>
        /// 写入数据
        /// </summary>
        /// <param name="send">数据</param>
        /// <param name="offSet">偏移量</param>
        /// <param name="count">字节数</param>
        public void WritePort(byte[] send, int offSet, int count)
        {
            if (IsOpen)
            {
                serialPort.Write(send, offSet, count);
            }
        }

        /// <summary>
        /// 获取可用串口
        /// </summary>
        /// <returns>串口名称列表</returns>
        public string[] GetComName()
        {
            string[] names = null;
            try
            {
                names = SerialPort.GetPortNames(); // 获取所有可用串口的名字
            }
            catch (Exception)
            {
                System.Windows.Forms.MessageBox.Show("找不到串口");
            }
            return names;
        }

        /// <summary>
        /// 注册一个串口
        /// </summary>
        /// <param name="portName">端口号</param>
        /// <param name="baudRate">波特率</param>
        public void SerialPortValue(string portName, int baudRate)
        {
            //串口名
            serialPort.PortName = portName;
            //波特率
            serialPort.BaudRate = baudRate;
            //数据位
            serialPort.DataBits = 8;
            //两个停止位
            serialPort.StopBits = System.IO.Ports.StopBits.One;
            //无奇偶校验位
            serialPort.Parity = System.IO.Ports.Parity.None;
            serialPort.ReadTimeout = 100;
            //commBar.serialPort.WriteTimeout = -1;
        }

        #region 实现IDisposable

        private bool m_disposed = false;//标识资源是否被释放过

        /// <summary>
        /// 释放
        /// </summary>
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);//防止Finalize调用
        }

        /// <summary>
        /// 释放
        /// </summary>
        /// <param name="disposing"></param>
        protected virtual void Dispose(bool disposing)
        {
            if (!m_disposed)
            {
                if (disposing)
                {
                    //释放托管资源
                    serialPort.Close();
                }
                //释放非托管资源
                m_disposed = true;
            }
        }

        /// <summary>
        /// 析构函数
        /// </summary>
        ~CommBar()
        {
            Dispose(false);
        }

        #endregion
    }
}
